package com.example.socialwebserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.socialwebserver.mapper.ActivityJoinMapper;
import com.example.socialwebserver.mapper.ActivityMapper;
import com.example.socialwebserver.mapper.CategoryMapper;
import com.example.socialwebserver.mapper.UserMapper;
import com.example.socialwebserver.pojo.*;
import com.example.socialwebserver.service.ActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    ActivityJoinMapper activityJoinMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public boolean createActivity(ActivityCreateDTO activityCreateDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",activityCreateDTO.getUserId());
        queryWrapper.eq("category_id",activityCreateDTO.getCategoryId());
        queryWrapper.eq("name",activityCreateDTO.getName());
        ActivityDAO selectOne = activityMapper.selectOne(queryWrapper);
        if(selectOne != null)
        {
            throw new RuntimeException("已经发起过该活动");
        }
        ActivityDAO activityDAO = new ActivityDAO();
        BeanUtils.copyProperties(activityCreateDTO,activityDAO);
        int insertRow = activityMapper.insert(activityDAO);
        return insertRow>=1?true:false;
    }

    @Override
    public boolean updateActivity(ActivityUpdateDTO activityUpdateDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",activityUpdateDTO.getId());
        ActivityDAO activityDAO = new ActivityDAO();
        BeanUtils.copyProperties(activityUpdateDTO,activityDAO);
        int updateRow = activityMapper.update(activityDAO, queryWrapper);
        return updateRow>0?true:false;
    }

    @Override
    public boolean joinActivity(ActivityJoinDAO activityJoinDAO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("activity_id",activityJoinDAO.getActivityId());
        queryWrapper.eq("user_id",activityJoinDAO.getUserId());
        ActivityJoinDAO activityJoin = activityJoinMapper.selectOne(queryWrapper);
        if(activityJoin!=null) {
            throw new RuntimeException("你已经参加过该类型的活动");
        }
        queryWrapper.clear();
        queryWrapper.eq("id",activityJoinDAO.getActivityId());
        ActivityDAO activityDAO1 = activityMapper.selectOne(queryWrapper);
        if(activityDAO1.getCurrPeople() == activityDAO1.getNumPeople())
        {
            return false;
        }
        ActivityDAO activityDAO = new ActivityDAO();
        activityDAO.setCurrPeople(activityDAO1.getCurrPeople()+1);
        int res = activityMapper.update(activityDAO, queryWrapper);
        if(res>0) {
            int insertRow = activityJoinMapper.insert(activityJoinDAO);
            if(insertRow>0)return true;
            else {
                activityDAO.setCurrPeople(activityDAO1.getCurrPeople());
                int row = activityMapper.update(activityDAO,queryWrapper);
            }
        }
        return false;
    }

    @Override
    public boolean quitActivity(ActivityJoinDAO activityJoinDAO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("activity_id",activityJoinDAO.getActivityId());
        queryWrapper.eq("user_id",activityJoinDAO.getUserId());
        ActivityJoinDAO activityJoinDAO1 = activityJoinMapper.selectOne(queryWrapper);
        if(activityJoinDAO1==null)
        {
            throw new RuntimeException("你还未加入过该活动");
        }
        int deleteRow = activityJoinMapper.delete(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("id",activityJoinDAO.getActivityId());
        ActivityDAO activityDAO1 = activityMapper.selectOne(queryWrapper);
        ActivityDAO activityDAO = new ActivityDAO();
        activityDAO.setCurrPeople(activityDAO1.getCurrPeople()-1);
        int update = activityMapper.update(activityDAO, queryWrapper);
        return (deleteRow>0 && update>0)?true:false;
    }

    @Override
    public boolean deleteActivity(Integer activityId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",activityId);
        int rew = activityMapper.delete(queryWrapper);
        return rew>0;
    }

    @Override
    public List<ActivityVO> selectByClassId(Integer categoryId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id",categoryId);
        List<ActivityDAO> selectList = activityMapper.selectList(queryWrapper);
        ArrayList<ActivityVO> voList = new ArrayList<ActivityVO>();
        for(ActivityDAO activityDAO:selectList)
        {
            ActivityVO activityVO = new ActivityVO();
            BeanUtils.copyProperties(activityDAO,activityVO);
            queryWrapper.clear();
            queryWrapper.eq("id",categoryId);
            ActivityClassDAO activityClassDAO = categoryMapper.selectOne(queryWrapper);
            activityVO.setCategoryName(activityClassDAO.getCategory());
            queryWrapper.clear();
            queryWrapper.eq("id",activityDAO.getUserId());
            UserDAO userDAO = userMapper.selectOne(queryWrapper);
            activityVO.setUserName(userDAO.getName());
            voList.add(activityVO);
        }
        return voList;
    }

    @Override
    public ActivityVO selectById(Integer activityId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",activityId);
        ActivityDAO activityDAO = activityMapper.selectOne(queryWrapper);
        if(activityDAO==null)return null;
        queryWrapper.clear();
        ActivityVO activityVO = new ActivityVO();
        queryWrapper.eq("id",activityDAO.getCategoryId());
        BeanUtils.copyProperties(activityDAO,activityVO);
        ActivityClassDAO activityClassDAO = categoryMapper.selectOne(queryWrapper);
        activityVO.setCategoryName(activityClassDAO.getCategory());
        queryWrapper.clear();

        queryWrapper.eq("id",activityDAO.getUserId());
        UserDAO userDAO = userMapper.selectOne(queryWrapper);
        activityVO.setUserName(userDAO.getName());

        return activityVO;
    }

    @Override
    public List<ActivityVO> selectAll() {

        List<ActivityDAO> selectList = activityMapper.selectList(null);
        ArrayList<ActivityVO> voList = new ArrayList<ActivityVO>();
        for(ActivityDAO activityDAO:selectList)
        {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",activityDAO.getCategoryId());
            ActivityVO activityVO = new ActivityVO();
            BeanUtils.copyProperties(activityDAO,activityVO);
            ActivityClassDAO activityClassDAO = categoryMapper.selectOne(queryWrapper);
            activityVO.setCategoryName(activityClassDAO.getCategory());
            queryWrapper.clear();
            queryWrapper.eq("id",activityDAO.getUserId());
            UserDAO userDAO = userMapper.selectOne(queryWrapper);
            activityVO.setUserName(userDAO.getName());
            voList.add(activityVO);
        }
        return voList;
    }
}
